package com.copa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copa.dao.CommandDao;
import com.copa.entity.Command;
import com.copa.service.CommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author copa
 * @createDate 2022-02-24 21:45
 * @function
 */
@Service
public class CommandServiceImpl implements CommandService {

    @Autowired
    private CommandDao commandDao;

    @Override
    public boolean findCountByCommandAndType(String command, Byte type) {
        QueryWrapper<Command> commandQueryWrapper = new QueryWrapper<>();
        commandQueryWrapper.likeRight("command", command.length() > 3 ? command.substring(0, 3) : command)
                .eq("type", type)
                .eq("enable", true)
                .getSqlSelect();
        return commandDao.selectCount(commandQueryWrapper) > 0;
    }

    @Override
    public String findCommandByCommandAndType(String command, Byte type) {
        QueryWrapper<Command> commandQueryWrapper = new QueryWrapper<>();
        commandQueryWrapper.eq("command", command)
                .eq("type", type)
                .eq("enable", true)
                .select("remark");
        Command commandEntity = commandDao.selectOne(commandQueryWrapper);
        return null != commandEntity ? commandEntity.getRemark() : null;
    }
}
